package com.zrrd.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.common.result.Result;
import com.zrrd.model.product.SpuImage;
import com.zrrd.model.product.SpuInfo;
import com.zrrd.model.product.SpuSaleAttr;
import com.zrrd.product.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    //分页查询Spu列表
    @GetMapping("/{page}/{limit}")
    public Result<Page> page(@PathVariable("page") Integer page,
                             @PathVariable("limit") Integer limit,
                             Long category3Id){
        //创建分页对象
        Page pageInfo = new Page(page,limit);
        //创建条件构造器
        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id" , category3Id);
        //执行分页
        spuInfoService.page(pageInfo, wrapper);
        //返回结果
        return Result.ok(pageInfo);
    }

    //Spu保存
    @PostMapping("/saveSpuInfo")
    public Result<String> saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuInfoService.saveSpu(spuInfo);
        return Result.ok("保存成功");
    }

    //根据SpuId获取Spu关联的图片列表
    @GetMapping("/spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable("spuId") Long spuId){
        return Result.ok(spuInfoService.getSpuImage(spuId));
    }

    //根据SpuId获取Spu销售属性以及属性值
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrList(@PathVariable("spuId") Long spuId){
        return Result.ok(spuInfoService.spuSaleAttrList(spuId));
    }

}
